<template>
	<view class="content">
		<view class="list">
			<view>
				<radio-group class="list-item" @click="radioChange(item)" v-for="(item, index) in list" :key="index">
					<view class="left">
						<label class="uni-list-cell">
							<view>
								<radio :value="item.id+''" :checked="chooseId === item.id" style="transform:scale(0.7)" :color="color"></radio>
							</view>
							<view>
								<label class="label-2-text">
									<view class="">
										<text class="name">{{item.name}}</text>{{item.mobile}} <text class="default" v-if="item.is_default == 2">(默认地址)</text></view>
								</label>
							</view>
						</label>

						<view>{{item.province_text}}{{item.city_text}}{{item.area_text}}{{item.street_text}}{{item.posaddress}}{{item.address}}</view>
					</view>
					<view class="right" @tap.stop="toAddressEdit(item)">
						<image src="/static/img/bian_icon.png"></image>
					</view>
				</radio-group>
			</view>

		</view>
		<button class="common-btn" @click="toPage('/pages/member/addAddress')">新增地址</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				color: '#FF6001',
				chooseId: 0,
				adds: '' //控制返回
			}
		},
		onShow() {
			this.getAddressList();
			if(getApp().globalData.address_id){
				this.chooseId = getApp().globalData.address_id
			}
		},
		onLoad(options) {
			this.adds = options.adds;
		},
		onNavigationBarButtonTap(e){
			if(e == 4){
				if (this.adds != 3) {
					uni.navigateBack();
				}else{
					getApp().globalData.index = 4
					uni.redirectTo({
						url:"/pages/index/index"
					})
				}
			}
		},
		methods: {
			toPage(url){
				uni.navigateTo({
					url
				})
			},
			getAddressList() {
				this.$http.address().then((res) => {
					console.log(res)
					if (res.code == 0) {
						this.list = res.data;
						if(res.data && res.data.length>0){
							res.data.forEach((val) => {
								if(val.is_default == 2){
									if(!getApp().globalData.address_id){
										this.chooseId = val.id
									}
								}
							})
						}
					}
				})
			},
			// 编辑地址
			toAddressEdit(address) {
				uni.navigateTo({
					url: `/pages/member/addAddress?address=${JSON.stringify(address)}`
				})
			},
			// // 选择地址
			// selectAddress(address) {
			// 	uni.setStorageSync('address',JSON.stringify(address))
			// 	uni.navigateBack();
			// },
			radioChange: function(item) {
				this.chooseId = item.id;
				if (this.adds != 3) {
					getApp().globalData.address = item.posaddress+item.address
					getApp().globalData.address_id = item.id
					uni.navigateBack();
				}

			}
		}
	}
</script>

<style scoped>
	.content {
		width: 100%;
		background: #fff;
	}

	.list {
		display: flex;
		flex-direction: column;
		padding: 10px 4%;
	}

	.uni-list {
		display: flex;
		flex-direction: row;
		align-items: center;
		width: 100%;
		justify-content: space-between;
	}

	.list-item {
		padding: 20px 4%;
		margin-bottom: 10px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: rgba(187, 187, 187, 0.1);
		border-radius: 5px;
		color: #787878;
		line-height: 24px;
	}

	.list-item .left .name {
		margin-right: 10px;
		font-size: 14px;
		font-weight: bold;
		color: #111110;
	}

	.right image {
		width: 20px;
		height: 20px;
	}

	.list-item .icon {
		color: #FF5300;
	}

	.default {
		width: 138upx;
		height: 48upx;
		border-radius: 30upx;
		border: 1px solid rgba(254, 71, 68, 1);
		font-weight: normal;
		font-size: 12px;
		margin-left: 10px;
		padding: 2px 10px;
		color: #FE4744;
		white-space: pre;
	}

	.common-btn {
		
background: linear-gradient(90deg, #FFC500 0%, #FF5F01 100%);
		
box-shadow: 0px 4px 8px 0px #FFAB71;
		border-radius: 48upx;
		height: 96upx;
		width: 100%;
		color: #FFFFFF;
		font-size: 16px;
		line-height: 96upx;
		position: fixed;
		bottom: 10px;
	}

	.uni-list-cell {
		display: flex;
		flex-direction: row;
	}
</style>
